package com.ruoyi.app.strategy.impl;

import com.ruoyi.app.domain.constants.CustomPromptConstant;
import com.ruoyi.app.domain.entity.User;
import com.ruoyi.app.domain.params.UserLoginParam;
import com.ruoyi.app.mapper.UserMapper;
import com.ruoyi.app.strategy.LoginStrategy;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.sign.Md5Utils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class AccountLoginStrategyImpl implements LoginStrategy {

    @Resource
    private UserMapper userMapper;

    @Override
    public User login(UserLoginParam userLoginParam) throws ServiceException {
        User user = new User();

        // 设置登录凭证信息
        user.setCode(userLoginParam.getCode());
        user.setPassword(Md5Utils.hash(userLoginParam.getPassword()));

        // 查询用户信息
        user = userMapper.selectByCodeAndPassword(user);

        if (user == null) {
            throw new ServiceException(CustomPromptConstant.ACCOUNT_PASSWORD_ERROR, HttpStatus.FORBIDDEN);
        }

        return user;
    }
}
